﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SGS.Standard
{
    public class PlayerDistanceCollection : List<PlayerDistance>
    {
        public int GetDistance( Player from, Player to )
        {
            if (from == to)
            {
                return 0;
            }

            var entity = this.Where( p => p.From == from && p.To == to ).FirstOrDefault();
            return entity == null ? int.MaxValue : entity.Distance;
        }

        public static PlayerDistanceCollection Calc( Player fromPlayer )
        {
            return Calc( GamePool.Current.PlayersCircle, fromPlayer );
        }

        public static int GetPlayerDistance(Player from, Player to)
        {
            return Calc(GamePool.Current.PlayersCircle, from).GetDistance(from, to);
        }

        public static PlayerDistanceCollection Calc( PlayerCollection pc, Player fromPlayer )
        {
            PlayerDistanceCollection obj = new PlayerDistanceCollection();

            if (!pc.Contains( fromPlayer ))
            {
                return obj;
            }
            else
            {
                int range = 0;
                DataStruct.CircleNode<Player> dummy = null;
                DataStruct.CircleNode<Player> currentNode = pc.GetCircleNode( fromPlayer );
                int distanceToOtherOffset = currentNode.Value.GameInfo.DistanceToOtherOffset;
                // int distanceFromOtherOffset = currentNode.Value.GameInfo.DistanceFromOtherOffset;
                                
                Dictionary<Player, int> dictForwardRange = new Dictionary<Player, int>();
                Dictionary<Player, int> dictReverseRange = new Dictionary<Player, int>();

                // 正向
                range = 0;
                dummy = currentNode;
                while ( dummy.Next != currentNode )
                {
                    dummy = dummy.Next;
                    if (!dummy.Value.GameInfo.Live)
                    {
                        continue;
                    }

                    range++;
                    dictForwardRange[dummy.Value]  = range - distanceToOtherOffset + dummy.Value.GameInfo.DistanceFromOtherOffset;
                }

                // 反向
                range = 0;
                dummy = currentNode;
                while (dummy.Previous != currentNode)
                {
                    dummy = dummy.Previous;
                    if (!dummy.Value.GameInfo.Live)
                    {
                        continue;
                    }

                    range++;
                    dictReverseRange[dummy.Value] = range - distanceToOtherOffset + dummy.Value.GameInfo.DistanceFromOtherOffset;
                }

                // 取距离的较小值
                var query = from a in dictReverseRange
                            from b in dictForwardRange
                            where a.Key == b.Key
                            select new PlayerDistance(fromPlayer, a.Key, a.Value > b.Value ? b.Value : a.Value);

                obj.AddRange(query.ToList());
            }

            return obj;
        }
    }
}
